/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controlador.autogenerats;

import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Fitosanitari;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.UnitatsMesura;
import entity.Tractament;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author David
 */
public class FitosanitariJpaController implements Serializable {

    public FitosanitariJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Fitosanitari fitosanitari) {
        if (fitosanitari.getTractamentCollection() == null) {
            fitosanitari.setTractamentCollection(new ArrayList<Tractament>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UnitatsMesura unitatsDisolucio = fitosanitari.getUnitatsDisolucio();
            if (unitatsDisolucio != null) {
                unitatsDisolucio = em.getReference(unitatsDisolucio.getClass(), unitatsDisolucio.getId());
                fitosanitari.setUnitatsDisolucio(unitatsDisolucio);
            }
            UnitatsMesura unitatsDosis = fitosanitari.getUnitatsDosis();
            if (unitatsDosis != null) {
                unitatsDosis = em.getReference(unitatsDosis.getClass(), unitatsDosis.getId());
                fitosanitari.setUnitatsDosis(unitatsDosis);
            }
            Collection<Tractament> attachedTractamentCollection = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionTractamentToAttach : fitosanitari.getTractamentCollection()) {
                tractamentCollectionTractamentToAttach = em.getReference(tractamentCollectionTractamentToAttach.getClass(), tractamentCollectionTractamentToAttach.getId());
                attachedTractamentCollection.add(tractamentCollectionTractamentToAttach);
            }
            fitosanitari.setTractamentCollection(attachedTractamentCollection);
            em.persist(fitosanitari);
            if (unitatsDisolucio != null) {
                unitatsDisolucio.getFitosanitariCollection().add(fitosanitari);
                unitatsDisolucio = em.merge(unitatsDisolucio);
            }
            if (unitatsDosis != null) {
                unitatsDosis.getFitosanitariCollection().add(fitosanitari);
                unitatsDosis = em.merge(unitatsDosis);
            }
            for (Tractament tractamentCollectionTractament : fitosanitari.getTractamentCollection()) {
                Fitosanitari oldFitosanitariIdOfTractamentCollectionTractament = tractamentCollectionTractament.getFitosanitariId();
                tractamentCollectionTractament.setFitosanitariId(fitosanitari);
                tractamentCollectionTractament = em.merge(tractamentCollectionTractament);
                if (oldFitosanitariIdOfTractamentCollectionTractament != null) {
                    oldFitosanitariIdOfTractamentCollectionTractament.getTractamentCollection().remove(tractamentCollectionTractament);
                    oldFitosanitariIdOfTractamentCollectionTractament = em.merge(oldFitosanitariIdOfTractamentCollectionTractament);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Fitosanitari fitosanitari) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fitosanitari persistentFitosanitari = em.find(Fitosanitari.class, fitosanitari.getId());
            UnitatsMesura unitatsDisolucioOld = persistentFitosanitari.getUnitatsDisolucio();
            UnitatsMesura unitatsDisolucioNew = fitosanitari.getUnitatsDisolucio();
            UnitatsMesura unitatsDosisOld = persistentFitosanitari.getUnitatsDosis();
            UnitatsMesura unitatsDosisNew = fitosanitari.getUnitatsDosis();
            Collection<Tractament> tractamentCollectionOld = persistentFitosanitari.getTractamentCollection();
            Collection<Tractament> tractamentCollectionNew = fitosanitari.getTractamentCollection();
            List<String> illegalOrphanMessages = null;
            for (Tractament tractamentCollectionOldTractament : tractamentCollectionOld) {
                if (!tractamentCollectionNew.contains(tractamentCollectionOldTractament)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tractament " + tractamentCollectionOldTractament + " since its fitosanitariId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (unitatsDisolucioNew != null) {
                unitatsDisolucioNew = em.getReference(unitatsDisolucioNew.getClass(), unitatsDisolucioNew.getId());
                fitosanitari.setUnitatsDisolucio(unitatsDisolucioNew);
            }
            if (unitatsDosisNew != null) {
                unitatsDosisNew = em.getReference(unitatsDosisNew.getClass(), unitatsDosisNew.getId());
                fitosanitari.setUnitatsDosis(unitatsDosisNew);
            }
            Collection<Tractament> attachedTractamentCollectionNew = new ArrayList<Tractament>();
            for (Tractament tractamentCollectionNewTractamentToAttach : tractamentCollectionNew) {
                tractamentCollectionNewTractamentToAttach = em.getReference(tractamentCollectionNewTractamentToAttach.getClass(), tractamentCollectionNewTractamentToAttach.getId());
                attachedTractamentCollectionNew.add(tractamentCollectionNewTractamentToAttach);
            }
            tractamentCollectionNew = attachedTractamentCollectionNew;
            fitosanitari.setTractamentCollection(tractamentCollectionNew);
            fitosanitari = em.merge(fitosanitari);
            if (unitatsDisolucioOld != null && !unitatsDisolucioOld.equals(unitatsDisolucioNew)) {
                unitatsDisolucioOld.getFitosanitariCollection().remove(fitosanitari);
                unitatsDisolucioOld = em.merge(unitatsDisolucioOld);
            }
            if (unitatsDisolucioNew != null && !unitatsDisolucioNew.equals(unitatsDisolucioOld)) {
                unitatsDisolucioNew.getFitosanitariCollection().add(fitosanitari);
                unitatsDisolucioNew = em.merge(unitatsDisolucioNew);
            }
            if (unitatsDosisOld != null && !unitatsDosisOld.equals(unitatsDosisNew)) {
                unitatsDosisOld.getFitosanitariCollection().remove(fitosanitari);
                unitatsDosisOld = em.merge(unitatsDosisOld);
            }
            if (unitatsDosisNew != null && !unitatsDosisNew.equals(unitatsDosisOld)) {
                unitatsDosisNew.getFitosanitariCollection().add(fitosanitari);
                unitatsDosisNew = em.merge(unitatsDosisNew);
            }
            for (Tractament tractamentCollectionNewTractament : tractamentCollectionNew) {
                if (!tractamentCollectionOld.contains(tractamentCollectionNewTractament)) {
                    Fitosanitari oldFitosanitariIdOfTractamentCollectionNewTractament = tractamentCollectionNewTractament.getFitosanitariId();
                    tractamentCollectionNewTractament.setFitosanitariId(fitosanitari);
                    tractamentCollectionNewTractament = em.merge(tractamentCollectionNewTractament);
                    if (oldFitosanitariIdOfTractamentCollectionNewTractament != null && !oldFitosanitariIdOfTractamentCollectionNewTractament.equals(fitosanitari)) {
                        oldFitosanitariIdOfTractamentCollectionNewTractament.getTractamentCollection().remove(tractamentCollectionNewTractament);
                        oldFitosanitariIdOfTractamentCollectionNewTractament = em.merge(oldFitosanitariIdOfTractamentCollectionNewTractament);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = fitosanitari.getId();
                if (findFitosanitari(id) == null) {
                    throw new NonexistentEntityException("The fitosanitari with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Fitosanitari fitosanitari;
            try {
                fitosanitari = em.getReference(Fitosanitari.class, id);
                fitosanitari.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The fitosanitari with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Tractament> tractamentCollectionOrphanCheck = fitosanitari.getTractamentCollection();
            for (Tractament tractamentCollectionOrphanCheckTractament : tractamentCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Fitosanitari (" + fitosanitari + ") cannot be destroyed since the Tractament " + tractamentCollectionOrphanCheckTractament + " in its tractamentCollection field has a non-nullable fitosanitariId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            UnitatsMesura unitatsDisolucio = fitosanitari.getUnitatsDisolucio();
            if (unitatsDisolucio != null) {
                unitatsDisolucio.getFitosanitariCollection().remove(fitosanitari);
                unitatsDisolucio = em.merge(unitatsDisolucio);
            }
            UnitatsMesura unitatsDosis = fitosanitari.getUnitatsDosis();
            if (unitatsDosis != null) {
                unitatsDosis.getFitosanitariCollection().remove(fitosanitari);
                unitatsDosis = em.merge(unitatsDosis);
            }
            em.remove(fitosanitari);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Fitosanitari> findFitosanitariEntities() {
        return findFitosanitariEntities(true, -1, -1);
    }

    public List<Fitosanitari> findFitosanitariEntities(int maxResults, int firstResult) {
        return findFitosanitariEntities(false, maxResults, firstResult);
    }

    private List<Fitosanitari> findFitosanitariEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Fitosanitari.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Fitosanitari findFitosanitari(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Fitosanitari.class, id);
        } finally {
            em.close();
        }
    }

    public int getFitosanitariCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Fitosanitari> rt = cq.from(Fitosanitari.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
